#include "timeeditdelegate.h"

TimeEditDelegate::TimeEditDelegate(QObject *parent) :
    QItemDelegate(parent)
{
}
QWidget *TimeEditDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &/* option */,
    const QModelIndex &/* index */) const
{
    QTimeEdit *editor = new QTimeEdit(parent);
    editor->setAutoFillBackground(true);
    editor->setDisplayFormat("hh:mm");

//    editor->setMinimum(0);
//    editor->setMaximum(100);

    return editor;
}

void TimeEditDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex &index) const
{
    QTime value = index.model()->data(index, Qt::EditRole).toTime();

    QTimeEdit *timeEdit = static_cast<QTimeEdit*>(editor);
    timeEdit->setDisplayFormat("hh:mm");
    timeEdit->setTime(value);
}

void TimeEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &index) const
{
    QTimeEdit *timeEdit = static_cast<QTimeEdit*>(editor);
    timeEdit->setDisplayFormat("hh:mm");
//    comboBox->interpretText();
    QTime value = timeEdit->time();

    model->setData(index, value, Qt::EditRole);
}

void TimeEditDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
    editor->setGeometry(option.rect);
}
